<script>
    var switchId;
    function nextPage(pageNo){
        $("#switch-listing").load("<?= $this->baseURI(); ?>planning/switch/list/0/"+pageNo);
    }
    function editSwitch(id){
        switchId = id;
        $("#edit-switch-form").load("<?= $this->baseURI(); ?>planning/switch/edit/"+id);
        $("#edit-switch-form" ).dialog( "open" );
    }
    function switchDetail(id,name){
        switchId = id;
        $("#switch-detail").attr('title', name+' Detail');
        $(function() {	           
            $( "#dialog:ui-dialog" ).dialog( "destroy" );
            $( "#switch-detail" ).dialog({
                resizable: false,
                
                width:550,
                modal: true
                
            });
            $("#switch-detail").load("<?= $this->baseURI(); ?>planning/switch/switchdetail/"+id);
        });      
        
    }
    function deleteSwitch(id){
                
        $(function() {	
           
            $( "#dialog:ui-dialog" ).dialog( "destroy" );
            $( "#delete-confirm" ).dialog({
                resizable: false,
                height:200,
                modal: true,
                buttons: {
                    "Delete": function() {
                        $.ajax({
                            type: "POST",
                            url: "<?= $this->baseURI(); ?>planning/switch/delete/"+id,                            
                            success: function(msg){
                                $("#switch-listing").load("<?= $this->baseURI(); ?>planning/switch/list");
                            }
                        }); 
                        $( this ).dialog( "close" );
                    },
                    Cancel: function() {
                        $( this ).dialog( "close" );
                    }
                }
            });
            
        });
    }
    
    $(function() {		
        $( "#dialog:ui-dialog" ).dialog( "destroy" );        

        function updateTips( t ) {
            tips = $( ".validateTips" );
            tips
            .text( t )
            .addClass( "ui-state-highlight" );
            setTimeout(function() {
                tips.removeClass( "ui-state-highlight", 1500 );
            }, 500 );
        }

        function checkLength( o, n, min, max ) {
            
            if ( o.val().length > max || o.val().length < min ) {
                o.addClass( "ui-state-error" );
                updateTips( "Length of " + n + " must be between " +
                    min + " and " + max + "." );
                return false;
            } else {
                return true;
            }
        }     
        
        function checkEmpty( o, n, min, max ) {
            if ( o.val().length < 1 ) {
                o.addClass( "ui-state-error" );
                updateTips( n + " must not be between empty");
                return false;
            } else {
                return true;
            }
        }
       
        $( "#add-switch-form" ).dialog({
            autoOpen: false,           
            width: 750,
            modal: true,
            buttons: {
                "Add": function() {
                    var bValid = true;
                    var name = $( "#switchName" ),
                    cabinet = $("#cabinetList"),
                    status = $( "#status" ),
                    odf = $("#odf"),
                    port = $( "#port" ),
                    datum_patch = $("#datum_patch"),
                    tomtprojektering = $("#tomtprojektering"),
                    anmarkning_tomt = $( "#anmarkning_tomt" ),
                    gravd_datum = $("#gravd_datum"),
                    gravd_av = $( "#gravd_av" ),
                    gravd_meter = $("#gravd_meter"),
                    gravd_timmar = $( "#gravd_timmar" ),
                    kopplad = $("#kopplad"),
                    vagschakt_projekterad = $( "#vagschakt_projekterad" ),
                    vagschakt_avropad = $("#vagschakt_avropad"),
                    vagschakt_klar = $( "#vagschakt_klar" ),
                    
                    allFields = $( [] ).add( name ).add( status ).add(cabinet),
                    tips = $( ".validateTips" );
                    
                    allFields.removeClass( "ui-state-error" );

                    bValid = bValid && checkLength( name, "switch name", 2, 16 );
                    bValid = bValid && checkEmpty( cabinet, "cabinet name", 2, 16 );
                                        
                    if ( bValid ) {
                        $.ajax({
                            type: "POST",
                            url: "<?= $this->baseURI(); ?>planning/switch/add",
                            data: ({switchName : name.val(),cabinetList:cabinet.val(),status : status.val(),tomtprojektering:tomtprojektering.val(),anmarkning_tomt:anmarkning_tomt.val(),gravd_datum:gravd_datum.val(),gravd_av:gravd_av.val(),
                                gravd_meter:gravd_meter.val(),gravd_timmar:gravd_timmar.val(),kopplad:kopplad.val(),vagschakt_projekterad:vagschakt_projekterad.val(),vagschakt_avropad:vagschakt_avropad.val(),vagschakt_klar:vagschakt_klar.val(),
                                odf:odf.val(),port:port.val(),datum_patch:datum_patch.val()
                            }),
                            success: function(msg){
                                $("#switch-listing").load("<?= $this->baseURI(); ?>planning/switch/list");
                            }
                        });                       
                        $( this ).dialog( "close" );
                        $('#add-switch-form').html('');
                    }
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                    $('#add-switch-form').html('');
                }
            },
            close: function() {
                $('#add-switch-form').html('');
                //   allFields.val( "" ).removeClass( "ui-state-error" );
            }
        });
        
        $( "#edit-switch-form" ).dialog({
            autoOpen: false,            
            width: 750,
            modal: true,
            buttons: {
                "Edit": function() {
                    var bValid = true;
                    var name = $( "#switchName" ),
                    cabinet = $("#cabinetList"),
                    status = $( "#status" ),
                    odf = $("#odf"),
                    port = $( "#port" ),
                    datum_patch = $("#datum_patch"),
                    tomtprojektering = $("#tomtprojektering"),
                    anmarkning_tomt = $( "#anmarkning_tomt" ),
                    gravd_datum = $("#gravd_datum"),
                    gravd_av = $( "#gravd_av" ),
                    gravd_meter = $("#gravd_meter"),
                    gravd_timmar = $( "#gravd_timmar" ),
                    kopplad = $("#kopplad"),
                    vagschakt_projekterad = $( "#vagschakt_projekterad" ),
                    vagschakt_avropad = $("#vagschakt_avropad"),
                    vagschakt_klar = $( "#vagschakt_klar" ),
                    
                    allFields = $( [] ).add( name ).add( status ).add(cabinet),
                    tips = $( ".validateTips" );
                    
                    allFields.removeClass( "ui-state-error" );

                    bValid = bValid && checkLength( name, "switch name", 2, 16 );
                    bValid = bValid && checkEmpty( cabinet, "cabinet name", 2, 16 );
                    
                    if ( bValid ) {
                        $.ajax({
                            type: "POST",
                            url: "<?= $this->baseURI(); ?>planning/switch/edit/"+switchId,
                            data: ({switchName : name.val(),cabinetList:cabinet.val(),status : status.val(),tomtprojektering:tomtprojektering.val(),anmarkning_tomt:anmarkning_tomt.val(),gravd_datum:gravd_datum.val(),gravd_av:gravd_av.val(),
                                gravd_meter:gravd_meter.val(),gravd_timmar:gravd_timmar.val(),kopplad:kopplad.val(),vagschakt_projekterad:vagschakt_projekterad.val(),vagschakt_avropad:vagschakt_avropad.val(),vagschakt_klar:vagschakt_klar.val(),
                                odf:odf.val(),port:port.val(),datum_patch:datum_patch.val()
                            }),
                            success: function(msg){
                                $("#switch-listing").load("<?= $this->baseURI(); ?>planning/switch/list");
                            }
                        });                       
                        $( this ).dialog( "close" );
                        $('#edit-switch-form').html('');
                    }
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                    $('#edit-switch-form').html('');
                }
            },
            close: function() {
                $('#edit-switch-form').html('');
                //   allFields.val( "" ).removeClass( "ui-state-error" );
            }
        });

    });
    
    function deleteFile(fileId,switchId){
        $.ajax({
            type: "POST",
            url: "<?= $this->baseURI(); ?>planning/switch/deletefile/"+fileId,
            data: ({}),
            success: function(msg){
                $("#switch-detail").load("<?= $this->baseURI(); ?>planning/switch/switchdetail/"+switchId);
            }
        });
    }
</script>
<div class="dataBoxOver">
    <div class="dataInnRep">
        <div class="dataInnTop" id="switch-listing">
            <?php echo $this->render('switch/list.phtml'); ?>	
        </div>	
    </div>
</div>
<div id="add-switch-form" title="Add Switch"></div>
<div id="edit-switch-form" title="Edit Switch"></div>
<div id="switch-detail"></div>
<div id="delete-confirm" title="Delete Switch" style="display: none;">
    <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This switch will be permanently deleted. Are you sure?</p>
</div>